﻿using Koala.Pro.CodeGenerator.UI.Common;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;
using System.Collections;

namespace Koala.Pro.CodeGenerator.UI
{
    public partial class FrmSelectTables : Form
    {
        public FrmSelectTables()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            listSource.Items.Clear();
            listSelected.Items.Clear();
            try
            {
                string selectedDatabaseName = Program.global.MainView.ToolWin.GetSelectedDatabaseName();
                DatabaseInfo databaseInfo = Program.global.DbInstance.DatabaseList[selectedDatabaseName];
                if (databaseInfo.TableList == null || databaseInfo.TableList.Count <= 0)
                {
                    databaseInfo.ListTables();
                }
                List<string> list = new List<string>();
                SortedList sortedList = new SortedList();
                foreach (string key in databaseInfo.TableList.Keys)
                {
                    sortedList.Add(key, key);
                }
                foreach (DictionaryEntry item in sortedList)
                {
                    list.Add(item.Value.ToString());
                }
                foreach (string item2 in list)
                {
                    listSource.Items.Add(item2);
                }
            }
            catch
            {
                MessageUtil.ShowError("数据获取错误！");
            }
            string selectedTableName = Program.global.MainView.ToolWin.GetSelectedTableName();
            if (!string.IsNullOrEmpty(selectedTableName))
            {
                listSource.SelectedItem = selectedTableName;
                btnSelect_Click(null, null);
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (object item in listSource.Items)
            {
                listSelected.Items.Add(item.ToString());
            }
            listSource.Items.Clear();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            foreach (object selectedItem in listSource.SelectedItems)
            {
                listSelected.Items.Add(selectedItem.ToString());
            }
            for (int num = listSource.SelectedItems.Count - 1; num >= 0; num--)
            {
                listSource.Items.Remove(listSource.SelectedItems[num].ToString());
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (object selectedItem in listSelected.SelectedItems)
            {
                listSource.Items.Add(selectedItem.ToString());
            }
            for (int num = listSelected.SelectedItems.Count - 1; num >= 0; num--)
            {
                listSelected.Items.Remove(listSelected.SelectedItems[num].ToString());
            }
        }

        private void RemoveAll_Click(object sender, EventArgs e)
        {
            foreach (object item in listSelected.Items)
            {
                listSource.Items.Add(item.ToString());
            }
            listSelected.Items.Clear();
        }

        private void SelectTablesView_Load(object sender, EventArgs e)
        {
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (object item in listSelected.Items)
            {
                list.Add(item.ToString());
            }
            Program.global.SelectedTableNames = list;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void listSource_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnSelect_Click(null, null);
        }

        private void listSelected_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnRemove_Click(null, null);
        }
    }
}
